package com.han.shadow.customerapp.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.fragment.InfoFragment;
import com.han.shadow.customerapp.fragment.ItemsFragment;
import com.han.shadow.customerapp.fragment.OrdersFragment;
import com.han.shadow.customerapp.http.Connector;
import com.han.shadow.customerapp.mystatic.MyScreen;

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener{


    private RadioGroup rgBar;
    private RadioButton rbChannel;


    private ItemsFragment itemsFragment = null;
    private InfoFragment infoFragment = new InfoFragment();
    private OrdersFragment ordersFragment = null;
    private FragmentManager fManager;
    private FragmentTransaction fTransaction;




    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            MyScreen.dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(MyScreen.dm);


            itemsFragment = new ItemsFragment(this,getLayoutInflater());
            ordersFragment = new OrdersFragment(this,getLayoutInflater());
            initFragment();

            //test();
        }


        @Nullable
        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {


            return super.onCreateView(name, context, attrs);

        }

    public void initFragment(){
        fManager = getFragmentManager();
        fTransaction = fManager.beginTransaction();
        fTransaction.add(R.id.ly_content, itemsFragment);
        fTransaction.add(R.id.ly_content, ordersFragment);
        fTransaction.add(R.id.ly_content, infoFragment);
        fTransaction.commit();


        rgBar = (RadioGroup)findViewById(R.id.rg_tab_bar);
        rgBar.setOnCheckedChangeListener(this);

        //获取第一个单选按钮，并设置其为选中状态
        rbChannel = (RadioButton)findViewById(R.id.rb_items);
        rbChannel.setChecked(true);

        //test();

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.rb_items:
                    fTransaction.show(itemsFragment);
                break;
            case R.id.rb_orders:
                    fTransaction.show(ordersFragment);
                break;
            case R.id.rb_info:
                    fTransaction.show(infoFragment);
                break;
        }
        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(itemsFragment != null)fragmentTransaction.hide(itemsFragment);
        if(ordersFragment != null)fragmentTransaction.hide(ordersFragment);
        if(infoFragment != null)fragmentTransaction.hide(infoFragment);
    }
}
